//
//  BJTabBarViewController.m
//  JuJinHuiRong
//
//  Created by APPLE on 16/11/3.
//  Copyright © 2016年 APPLE. All rights reserved.
//

#import "BJTabBarViewController.h"
#import "BJHomePageViewController.h"
#import "BJStudyViewController.h"
#import "BJTuiJianViewController.h"
#import "BJMineViewController.h"
#import "BJMineViewController.h"

#define fontSize 16
@interface BJTabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation BJTabBarViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.delegate = self;
    Reachability *reachability=[Reachability reachabilityWithHostName:@"www.baidu.com"];
    //    注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChange:) name:kReachabilityChangedNotification object:nil];
    [reachability startNotifier];
    [self setUpChildVcs];
}

#pragma mark ---- 加网络监测
-(void)reachabilityChange:(NSNotification *)sender{
    Reachability *reachability=[sender object];
    if (![reachability isReachable]) {
        UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:@"提示" message:@"网络已断开,请检查网络状况" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAC=[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *alertBC=[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVC addAction:alertAC];
        [alertVC addAction:alertBC];
        [self presentViewController:alertVC animated:YES completion:nil];
    }else{
        if (reachability.isReachableViaWiFi) {
            NSLog(@"当前通过wifi使用");
        }else if (reachability.isReachableViaWWAN){
            UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:@"提示" message:@"当前通过2G/3G/4G连接,是否继续使用" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *alertAC=[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            UIAlertAction *alertBC=[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alertVC addAction:alertAC];
            [alertVC addAction:alertBC];
            [self presentViewController:alertVC animated:YES completion:nil];
        }
    }
}

- (void)setUpChildVcs{
   
//    [self setUpOneChildVc:[[UINavigationController alloc] initWithRootViewController:[[BJHomePageViewController alloc] init]] title:@"首页" image:@"shou_w" selectedImage:@"shou"];
    
    UIImage *image1 = [UIImage imageNamed:@"home_d"];
    image1 = [image1 imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    
    UIImage *image2 = [UIImage imageNamed:@"xue_d"];
    image2 = [image2 imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

    UIImage *image3 = [UIImage imageNamed:@"tui_dddd"];
    image3 = [image3 imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

    UIImage *image4 = [UIImage imageNamed:@"wo_d"];
    image4 = [image4 imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

    [self setUpOneChildVc:[[UINavigationController alloc] initWithRootViewController:[[BJHomePageViewController alloc] init]] title:@"首页" image:[UIImage imageNamed:@"home"] selectedImage:image1];

    
      [self setUpOneChildVc:[[UINavigationController alloc] initWithRootViewController:[[BJStudyViewController alloc] init]] title:[NSString stringWithFormat:@"学\"习\""""] image:[UIImage imageNamed:@"xue" ] selectedImage:image2];
    
    
    [self setUpOneChildVc:[[UINavigationController alloc] initWithRootViewController:[[BJTuiJianViewController alloc] init]] title:@"推荐" image:[UIImage imageNamed:@"tuiddd" ] selectedImage:image3];
 
    
    [self setUpOneChildVc:[[UINavigationController alloc] initWithRootViewController:[[BJMineViewController alloc] init]] title:@"我的" image:[UIImage imageNamed:@"wo" ] selectedImage:image4];
//    [self ]
}

- (void)setUpOneChildVc:(UIViewController *)childVc title:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage{
    childVc.tabBarItem.title = title;
    childVc.tabBarItem.image = image;
    childVc.tabBarItem.selectedImage = selectedImage;
//[child]
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor grayColor], UITextAttributeTextColor,
                                                    nil] forState:UIControlStateNormal];
    UIColor *titleHighlightedColor = BaseColor;
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       titleHighlightedColor, UITextAttributeTextColor,
                                                    nil] forState:UIControlStateSelected];
    // 字体大小 跟 normal
    NSMutableDictionary *attrnor = [NSMutableDictionary dictionary];
    
    // 设置字体
    attrnor[NSFontAttributeName] = FontVlaue(13);
    
    [childVc.tabBarItem setTitleTextAttributes:attrnor forState:UIControlStateNormal];
    
    
    
    [self addChildViewController:childVc];
}

//- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
////    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
////    NSString *idStr = [user objectForKey:kUSER_id];
////    NSLog(@"--tabbaritem.title--%@",viewController.tabBarItem.title);
//    
//    //这里我判断的是当前点击的tabBarItem的标题
//    if ([viewController.tabBarItem.title isEqualToString:@"我的"]) {
//        //如果用户ID存在的话，说明已登陆
//        if (idStr) {
//            return YES;
//        }else{
//            //跳到登录页面
//            BJLoginViewController *login = [[BJLoginViewController alloc] init];
//            //隐藏tabbar
//            login.hidesBottomBarWhenPushed = YES;
////            [((UINavigationController *)tabBarController.selectedViewController) pushViewController:login animated:YES];
//            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
//            [self presentViewController:nav animated:YES completion:nil];
//
////            [self presentViewController:login animated:YES completion:nil];
//            
//            return NO;
//        }
//    }else
//        return YES;
//}


- (void)setUpItemTitleTextAttrs{
    //设置normal状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = FontVlaue(15);
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    //设置selected状态下的文字属性
    NSMutableDictionary *selectedArrts= [NSMutableDictionary dictionary];
    selectedArrts[NSForegroundColorAttributeName] = [UIColor redColor];
    
    //利用Appearance对象同意设置文字属性
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedArrts forState:UIControlStateSelected];
    
}
- (void)hideTabBar {
    if (self.tabBarController.tabBar.hidden == YES) {
        return;
    }
    UIView *contentView;
    if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    else
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    contentView.frame = CGRectMake(contentView.bounds.origin.x,  contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
    self.tabBarController.tabBar.hidden = YES;
}

//-(void)viewWillAppear:(BOOL)animated{
//    self.tabBarController.tabBar.hidden = NO;
//}
//
//- (void)viewWillDisappear:(BOOL)animated{
//    self.tabBarController.tabBar.hidden = YES;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
